//
//  ZH_MineCourseAllViewController.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/10/22.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_MineCourseAllViewController.h"
#import "ZH_HomeCourceTableViewCell.h"
#import "ZH_PlayBackTableViewCell.h"
#import "ZH_MineViewModel.h"
#import "ZH_HomeLiveTableViewCell.h"
#import "ZH_HomeSourceInfoViewController.h"
@interface ZH_MineCourseAllViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,weak)UITableView *tableView;
@property (nonatomic,strong)ZH_MineViewModel *viewModel;
@end

@implementation ZH_MineCourseAllViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserface];
    [self initData];
}
- (void)cleanUpData{
    [self setViewModel:nil];
}
- (void)initUserface{
    self.view.backgroundColor = kCommonBackgroudColor;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.view);
        make.left.equalTo(self.view).offset(FITSCALE(11));
        make.right.equalTo(self.view).offset(FITSCALE(-11));
    }];
}
- (void)initData{
    [self.viewModel.myCourseParam setObject:self.typeString forKey:@"type"];
    WEAKSelf;
    [self.viewModel myCourseResultBlock:^(NSString *code, NSString *message, id data) {
        if ([code isEqualToString:@"0"]) {
            [weakSelf.tableView reloadData];
        }
        else{
            if ([code isEqualToString:@"1002"]) {
                [self showHint:@"登录过期"];
            }
            else{
                [weakSelf showHint:message];
            }
        }
    }];
}
#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.viewModel.appointmentArray.count;
    }
    else if (section == 1){
        return self.viewModel.offlineArray.count;
    }
    else if (section == 2){
        return self.viewModel.liveArray.count;
    }
    else if (section == 3){
        return self.viewModel.demandArray.count;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return FITSCALE(9);
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        ZH_HomeCourceTableViewCell *cell = [ZH_HomeCourceTableViewCell cellWithTableView:tableView];
        [cell setHiddenGrade];
        [cell updatessUI:self.viewModel.appointmentArray[indexPath.row]];
        return cell;
    }
    else if (indexPath.section == 1){
        ZH_HomeCourceTableViewCell *cell = [ZH_HomeCourceTableViewCell cellWithTableView:tableView];
        [cell setHiddenGrade];
        [cell updatessUI:self.viewModel.offlineArray[indexPath.row]];
        return cell;
    }
    else if(indexPath.section == 2){
        ZH_PlayBackTableViewCell *cell = [ZH_PlayBackTableViewCell cellWithTableView:tableView];
        if (self.viewModel.liveArray.count > 0) {
            [cell updataUI:self.viewModel.liveArray[indexPath.row]];
        }
        return cell;
    }
    else if (indexPath.section == 3){
        ZH_HomeLiveTableViewCell *cell = [ZH_HomeLiveTableViewCell cellWithTableView:tableView];
        [cell updataUIs:self.viewModel.demandArray[indexPath.row]];
        return cell;
    }
   return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.section == 3){
        NSString *string = [NSString stringWithFormat:@"%@",self.viewModel.demandArray[indexPath.row][@"course_id"]];
        if(string.length > 0){
            ZH_HomeSourceInfoViewController *vc = [[ZH_HomeSourceInfoViewController alloc] init];
            vc.idString = string;
            vc.typeStrings = @"2";
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}
#pragma mark - getter,setter
- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [UITableView createTableViewWithDelegateTarget:self];
        tableView.backgroundColor = kCommonBackgroudColor;
        tableView.separatorStyle =UITableViewCellSeparatorStyleNone;
        [tableView setCellAutoAdaptationForEstimatedRowHeight:FITSCALE(100)];
        [self.view addSubview:_tableView = tableView];
    }
    return _tableView;
}
- (ZH_MineViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[ZH_MineViewModel alloc] init];
    }
    return _viewModel;
}
@end
